/*
 * Authors: Matt Stenzel, Josh Harris, Jon Homan
 * IT 484 Fall 2008
 * */

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using StringTok;
using System.IO;
using System.Collections;
using System.Windows.Forms;

namespace MahjongTest
{
    abstract class BoardFactory
    {
        // Eventually make this a factory so we can choose between other board layouts
        /*
         * This method reads points in from a file and adds them to the ArrayList or ArrayLists.
         * */
        public static ArrayList getBoardLayout(String boardName)
        {
            ArrayList boardLayout = new ArrayList();
            ArrayList levelPoints = null;
            StreamReader sr;
            string line;
            try
            {

                sr = File.OpenText(boardName);
                int i = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    levelPoints = makeLevel(line);
                    boardLayout.Add(levelPoints);
                    i++;
                }
                sr.Close();
            }
            catch (DirectoryNotFoundException dnfe)
            {
                MessageBox.Show(dnfe.Message, "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (FileNotFoundException fnfe)
            {
                MessageBox.Show(fnfe.Message, "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            return boardLayout;
        }

        /*
         * This method cuts the coordinates up and places them into an ArrayList.
         * */
        private static ArrayList makeLevel(string line)
        {
            StringTokenizer levelTokenizer = new StringTokenizer(line, ",");
            int levelSize = levelTokenizer.CountTokens();

            ArrayList level = new ArrayList();
            int i = 0;

            while (levelTokenizer.HasMoreTokens())
            {
                string point = levelTokenizer.NextToken();
                StringTokenizer pointTokenizer = new StringTokenizer(point, " ");

                while (pointTokenizer.HasMoreTokens())
                {
                    //Console.Write("(" + pointTokenizer.NextToken() + ", " + pointTokenizer.NextToken() + ")");
                    int x = Int32.Parse(pointTokenizer.NextToken());
                    int y = Int32.Parse(pointTokenizer.NextToken());
                    level.Add(new Point(x, y));
                    i++;
                }
            }

            return level;
        }
    }
}
